package com.zhoug.demo.push;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;

import androidx.core.app.NotificationCompat;

import com.zhoug.push.core.PushMessage;

/**
 * @description: 类的简要描述
 * @author: 35574
 * @date: 2025/11/4 14:09
 */
public class AppNotificationManager {


    public static void createNotificationChannel(Context context, String channelId, String channelName) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
            mChannel.enableLights(true);
            mChannel.enableVibration(true);
            mChannel.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION),
                    Notification.AUDIO_ATTRIBUTES_DEFAULT);
            getNotificationManager(context).createNotificationChannel(mChannel);
        }
    }

    public static void sendNotification(Context context, String channelId, PushMessage message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
        builder.setContentTitle("收到消息");
        builder.setSmallIcon(R.mipmap.icon_launcher);
        builder.setAutoCancel(true);
        Intent target = new Intent();
        target.putExtra("message", message);
        target.setClassName(context.getPackageName(), NotificationDispatchActivity.class.getCanonicalName());
        target.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001,
                target, PendingIntent.FLAG_UPDATE_CURRENT, null);
        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();
        NotificationManager notificationManager = getNotificationManager(context);
        notificationManager.notify(101,notification);
    }

    public static NotificationManager getNotificationManager(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            return context.getSystemService(NotificationManager.class);
        } else {
            return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
    }


}
